import { API_BASE_URL, handleApiResponse, type ApiResponse } from './config'

// API响应数据结构
export interface ApiMediaItem {
  id: string
  name: string
  osType: string
  aesKey: string | null
  createTime: string
  mediaCostRate: number | null
}

export interface ApiResponse {
  records: ApiMediaItem[]
  totalRow: number
  pageNumber: number
  pageSize: number
}

export interface MediaPageParams {
  current: number
  size: number
  name?: string
}

// 获取媒体列表
export const fetchMediaData = async (params: MediaPageParams): Promise<ApiResponse> => {
  try {
    const response = await fetch(`${API_BASE_URL}/media/page`, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify(params)
    })
    
    if (!response.ok) {
      throw new Error(`HTTP error! status: ${response.status}`)
    }
    
    return await handleApiResponse<ApiResponse>(response)
  } catch (error) {
    console.error('获取媒体数据失败:', error)
    throw error
  }
}

// 新增媒体
export const createMedia = async (mediaData: any): Promise<void> => {
  try {
    const response = await fetch(`${API_BASE_URL}/media`, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify(mediaData)
    })
    
    if (!response.ok) {
      throw new Error(`HTTP error! status: ${response.status}`)
    }
    
    await handleApiResponse<void>(response)
  } catch (error) {
    console.error('新增媒体失败:', error)
    throw error
  }
}

// 更新媒体
export const updateMedia = async (id: string, mediaData: any): Promise<void> => {
  try {
    const response = await fetch(`${API_BASE_URL}/media/${id}`, {
      method: 'PUT',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify(mediaData)
    })
    
    if (!response.ok) {
      throw new Error(`HTTP error! status: ${response.status}`)
    }
    
    await handleApiResponse<void>(response)
  } catch (error) {
    console.error('更新媒体失败:', error)
    throw error
  }
}

// 删除媒体
export const deleteMedia = async (id: string): Promise<void> => {
  try {
    const response = await fetch(`${API_BASE_URL}/media/${id}`, {
      method: 'DELETE'
    })
    
    if (!response.ok) {
      throw new Error(`HTTP error! status: ${response.status}`)
    }
    
    await handleApiResponse<void>(response)
  } catch (error) {
    console.error('删除媒体失败:', error)
    throw error
  }
}

// 切换媒体状态
export const toggleMediaStatus = async (id: string, status: string): Promise<void> => {
  try {
    const response = await fetch(`${API_BASE_URL}/media/${id}/status`, {
      method: 'PUT',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({ status })
    })
    
    if (!response.ok) {
      throw new Error(`HTTP error! status: ${response.status}`)
    }
    
    await handleApiResponse<void>(response)
  } catch (error) {
    console.error('切换媒体状态失败:', error)
    throw error
  }
}

// 获取媒体列表
export const fetchMediaList = async (): Promise<void> => {
  try {
    const response = await fetch(`${API_BASE_URL}/media/list`, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
    })
    
    if (!response.ok) {
      throw new Error(`HTTP error! status: ${response.status}`)
    }
    
    return await handleApiResponse<void>(response)
  } catch (error) {
    console.error('获取媒体列表失败:', error)
    throw error
  }
}

// 更新媒体成本率
export const updateMediaCostRate = async (id: string, mediaCostRate: number): Promise<void> => {
  try {
    const response = await fetch(`${API_BASE_URL}/media/updateMediaCostRate`, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({ id, mediaCostRate })
    })
    
    if (!response.ok) {
      throw new Error(`HTTP error! status: ${response.status}`)
    }
    
    await handleApiResponse<void>(response)
  } catch (error) {
    console.error('更新媒体成本率失败:', error)
    throw error
  }
}